<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Truckacc extends CI_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('truckaccmodel');
	}


	public function exportCSV(){
		if ($this->input->get_post('gensubmit')){
			$this->generateCSV();
		}
		else{
		$date = $this->input->get_post('date')?$this->input->get_post('date'):date('Y-m-d');
		//$date = isset_or($this->input->post('date'),'testtest');
		$partnum = $this->input->get_post('partnum')?$this->input->get_post('partnum'):'';
		//$partnum = isset_or($this->input->post('partnum'),'');
		$partname = $this->input->get_post('partname')?$this->input->get_post('partname'):'';
		//$partname = isset_or($this->input->post('partname'),'');
		$depcode = $this->input->get_post('depcode')?$this->input->get_post('depcode'):'';
		//$depcode = isset_or($this->input->post('depcode'),'');
		$data['date'] = $date;
		$data['partnum'] = $partnum;
		$data['partname'] = $partname;
		$data['depcode'] = $depcode;
		
		$result = $this->truckaccmodel->get_dateRange_sum($date);
		
		
		$data['headertable'] = array('ชื่อลาน','น้ำหนักรวม','ราคาต่อหน่วย','รวมเงินสุทธิ','หักเงินรวม','รวมเงินคงเหลือ','รวมโปรโมชั่น','รวมเงินคงเหลือ+โปรโมชั่น');
		$data['resultarray'] = $result->result_array();
		
		
		$data['role_id']=$this -> session -> userdata('role_id');
		$this->load->view('truckacc/exportcsv',$data);
		}
	}
	

	public function generateCSV(){

		$date = isset_or($this->input->get_post('date'),'');
		$partnum = isset_or($this->input->get_post('partnum'),'');
		$partname = isset_or($this->input->get_post('partname'),'');
		$depcode = isset_or($this->input->get_post('depcode'),'');

		//$startdate = '2011-01-01';
		//$enddate = '2011-12-31';

		$result = $this->truckaccmodel->get_dateRange_partnum($date,$partnum,$partname,$depcode);

		header( 'Content-Type: text/csv' );
		header( 'Content-Disposition: attachment;filename='.date('Ymd-His'));
		$fp = fopen('php://output', 'w');
		if ($result->num_rows() > 0){
			$resultarray = $result->result_array();
			//header
			fputcsv($fp, array_keys($resultarray[0]));

			foreach ($resultarray as $row)
			{
				fputcsv($fp, $row);
			}
		}
		fclose($fp);
	}

	function query_to_csv($db_conn, $query, $filename, $attachment = false, $headers = true) {
			
		if($attachment) {
			// send response headers to the browser
			header( 'Content-Type: text/csv' );
			header( 'Content-Disposition: attachment;filename='.$filename);
			$fp = fopen('php://output', 'w');
		} else {
			$fp = fopen($filename, 'w');
		}
			
		$result = mysql_query($query, $db_conn) or die( mysql_error( $db_conn ) );
			
		if($headers) {
			// output header row (if at least one row exists)
			$row = mysql_fetch_assoc($result);
			if($row) {
				fputcsv($fp, array_keys($row));
				// reset pointer back to beginning
				mysql_data_seek($result, 0);
			}
		}
			
		while($row = mysql_fetch_assoc($result)) {
			fputcsv($fp, $row);
		}
			
		fclose($fp);
	}

function testpluseq(){
		$allsum = array();
		$allsum['sdf'] = '';
		$allsum['sdf'] += 'sldk';
		echo $allsum['sdf'];
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */